package com.csit.action;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;

import com.csit.entity.Customer;
import com.csit.service.CustomerService;
import com.csit.util.ResponseMsgUtil;
import com.csit.util.StringUtil;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("basePackage")
public class CustomerAction extends BaseAction implements ModelDriven<Customer> {

	private static final long serialVersionUID = -3557056558286773190L;

	private Customer customer = new Customer();
	
	@Resource
	private CustomerService customerService;
	
	public Customer getModel() {
		return customer;
	}
	
	public void add(){
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		if(StringUtil.isNull(customer.getCustomerCode())){
			msgUtil.setMsg("客户编号不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(StringUtil.isNull(customer.getCustomerName())){
			msgUtil.setMsg("客户名称不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(customerService.isEntityExist("customerCode", customer.getCustomerCode())){
			msgUtil.setMsg("客户编号已存在");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		customer.setShop(getShop());
		customerService.saveEntity(customer);
		msgUtil.setSuccess(true);
		ajaxJson(msgUtil.toJSON());
	}
	
	public void update(){
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		if(StringUtil.isNull(customer.getCustomerName())){
			msgUtil.setMsg("客户名称不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		customerService.updateEntity(customer);
		msgUtil.setSuccess(true);
		ajaxJson(msgUtil.toJSON());
	}
	
	public void select(){
		if(customer.getShop()==null){
			customer.setShop(getShop());
		}
		String jsonString = customerService.queryCustomer(page, rows, customer);
		ajaxJson(jsonString);
	}
	
	public void query(){
		String jsonString = customerService.queryCustomer(page, rows, customer);
		ajaxJson(jsonString);
	}

}
